import {SETSTATE} from './actionTypes.js'

let defaultState = {
  msgList: [
    {
      title:"标题1",
      content:'明天去相亲',
      isCompleted: false
    },
    {
      title:"标题2",
      content:'别忘了化个妆',
      isCompleted: false
    },
    {
      title:"标题3",
      content:'爱好单位ID加我的酒窝',
      isCompleted: true
    },
    {
      title:"标题4",
      content:'hahahaID加我的酒窝',
      isCompleted: false
    }
  ]
}
// 初始 刷新时 取缓存 赋值 给 state  存的名字就是 user
const storageState = localStorage.getItem('msg')
                      ?JSON.parse(localStorage.getItem('msg'))
                       :null;

if(storageState){
  defaultState = storageState
}

const reducer = (state=defaultState, action)=>{
  let newState = JSON.parse(JSON.stringify(state))
  switch (action.type){
    case SETSTATE:
    newState.msgList[action.index].isCompleted = true
    default:
      break;
  }
  // 同步  缓存
  localStorage.setItem('msg', JSON.stringify(newState))
  return newState
}

export default reducer